import { defineStore } from 'pinia';

// 定义贪吃蛇游戏状态store
export const useSnakeStore = defineStore('snake', {
  state: () => ({
    // 游戏状态
    status: 'waiting', // waiting, playing, gameover
    
    // 玩家信息
    userId: '',
    userName: '',
    roomId: '',
    players: [],
    
    // 游戏数据
    snakes: {},
    foods: [],
    scores: {},
    
    // 游戏设置
    gridSize: 16,
    cellSize: 20,
    
    // 玩家状态
    isReady: false,
    isGameStarted: false,
    isGameOver: false,
    
    // UI状态
    gameTip: '等待玩家加入...',
    isMobile: false
  }),

  getters: {
    /**
     * 获取当前玩家
     * @returns {Object|null} 当前玩家对象或null
     */
    currentPlayer(state) {
      return state.players.find(player => player.user_id === state.userId);
    },
    
    /**
     * 获取当前玩家分数
     * @returns {Number} 当前玩家分数
     */
    currentPlayerScore(state) {
      return state.scores[state.userId] || 0;
    },
    
    /**
     * 检查游戏是否进行中
     * @returns {Boolean} 游戏是否进行中
     */
    isGamePlaying(state) {
      return state.status === 'playing';
    }
  },

  actions: {
    /**
     * 初始化游戏状态
     * @param {Object} userInfo - 用户信息
     * @param {String} roomId - 房间ID
     */
    initialize(userInfo, roomId) {
      this.userId = userInfo.userId;
      this.userName = userInfo.userName;
      this.roomId = roomId;
      
      // 初始化游戏状态
      this.status = 'waiting';
      this.snakes = {};
      this.foods = [];
      this.scores = {};
      this.players = [];
      this.isReady = false;
      this.isGameStarted = false;
      this.isGameOver = false;
      this.gameTip = '等待玩家加入...';
    },
    
    /**
     * 更新玩家列表
     * @param {Array} players - 玩家列表
     */
    updatePlayerList(players) {
      this.players = players.map(player => ({
        user_id: player.user_id,
        user_name: player.user_name,
        ready: player.ready || false
      }));
    },
    
    /**
     * 更新玩家准备状态
     * @param {String} userId - 用户ID
     * @param {Boolean} ready - 准备状态
     */
    updatePlayerReadyStatus(userId, ready) {
      const player = this.players.find(p => p.user_id === userId);
      if (player) {
        player.ready = ready;
      }
    },
    
    /**
     * 更新游戏状态
     * @param {Object} gameState - 游戏状态对象
     */
    updateGameState(gameState) {
      this.status = gameState.status || this.status;
      this.snakes = gameState.snakes || this.snakes;
      this.foods = gameState.foods || this.foods;
      this.scores = gameState.scores || this.scores;
    },
    
    /**
     * 设置游戏开始
     */
    startGame() {
      this.status = 'playing';
      this.isGameStarted = true;
      this.isGameOver = false;
      this.gameTip = '游戏开始！';
    },
    
    /**
     * 设置游戏结束
     * @param {Object} result - 游戏结果
     */
    endGame(result) {
      this.status = 'gameover';
      this.gameTip = '游戏结束！';
      this.isGameOver = true;
      this.isGameStarted = false;
      
      // 如果有获胜者信息
      if (result && result.winner) {
        this.gameTip = `游戏结束，${result.winner}获胜！`;
      }
    },
    
    /**
     * 更新玩家蛇的方向
     * @param {String} userId - 用户ID
     * @param {String} direction - 方向
     */
    updateSnakeDirection(userId, direction) {
      if (this.snakes[userId]) {
        const snake = this.snakes[userId];
        
        // 防止180度转向
        if (
          (direction === 'UP' && snake.direction === 'DOWN') ||
          (direction === 'DOWN' && snake.direction === 'UP') ||
          (direction === 'LEFT' && snake.direction === 'RIGHT') ||
          (direction === 'RIGHT' && snake.direction === 'LEFT')
        ) {
          return;
        }
        
        snake.direction = direction;
      }
    },
    
    /**
     * 切换准备状态
     */
    toggleReadyStatus() {
      this.isReady = !this.isReady;
      this.gameTip = this.isReady ? '已准备' : '未准备';
    },
    
    /**
     * 重置游戏
     */
    resetGame() {
      this.status = 'waiting';
      this.snakes = {};
      this.foods = [];
      this.scores = {};
      this.isReady = false;
      this.isGameStarted = false;
      this.isGameOver = false;
      this.gameTip = '等待玩家加入...';
    }
  }
});